//
//  Plane.m
//  BeatPlane
//
//  Created by qianfeng on 15-5-6.
//  Copyright (c) 2015年 韩世民. All rights reserved.
//

#import "Plane.h"
#define kScreenSize [UIScreen mainScreen].bounds.size
#define kPlaneSpeed 5
@implementation Plane
//单例函数
//虾米那的函数 不管调用多少次 只会创建有且只有一个对象
+(instancetype)defaultPlane{
    static Plane *plane = nil;
    @synchronized(self){//同步操作 考虑线程安全
        if (plane == nil) {
            plane = [[self alloc] init];
        }
    }
    return plane;
}
//单例不释放
-(instancetype)init{
    if (self = [super init]) {
        //一旦 创建之后 飞机就应该有图片 和 位置大小
        self.image = [UIImage imageNamed:@"enemy.png"];
        self.frame = CGRectMake(kScreenSize.width/2-50/2, kScreenSize.height-50-50, 50, 50);
        _towards = kStop;
    }
    return self;
}
//准备左移
-(void)startMoveLeft{
    _towards = kLeft;
}
//真正左移
-(void)moveLeft{
//先获取当前 frame
    CGRect  frame = self.frame;
    if (frame.origin.x>0) {
        frame.origin.x -=kPlaneSpeed ;
    }
    self.frame =frame;
}
-(void)startMoveRight{
    _towards = kRight;
}
//
-(void)moveRight{
    CGRect  frame = self.frame;
    if (frame.origin.x<kScreenSize.width-self.frame.size.width) {
        frame.origin.x +=kPlaneSpeed ;
    }
    self.frame =frame;
}

-(void)stopMoveing{
    _towards = kStop;
}

-(void)planeAction{
    switch (_towards) {
        case kLeft:
            [self moveLeft];
            break;
            case kRight:
            [self moveRight];
            break;
        default:
            break;
    }
}

@end
